﻿/*!Important URLs. Must be set for new setup -jayson*/

(function () {

    /* Start of module name */
    var regForm = angular.module('registrationForm', []);
    /*var baseAddress = 'http://localhost:2071/api/'; */
    var baseAddress = 'http://app.enrollment.api.buy-me.ph/api/';

    var url = "";
    /* End of module name */


    /* Start of Controller */
    regForm.controller('RegistrationController', function PostController($scope, ServiceFactory) {
        $scope.editMode = false;
        $scope.Home_Show = false;
        $scope.listOfOptions = ['-Select-', 'Old', 'New'];
        $scope.ddlSelectType_selectedItemChanged = function () {
            $scope.regForm.ShowNewEntryForOld = false;
            $scope.regForm.ShowSearchBox = false;

            if ($scope.regForm.Type == 'Old') {
                $scope.regForm.ShowNewEntryForOld = false; //this feature is has been disabled
                $scope.regForm.NewEntryForOldValue = false;
                $scope.chkNewEntryForOld_selectedItemChanged();
            }
            else if ($scope.regForm.Type == 'New') {
                $scope.regForm.ShowSearchBox = true;
            }
        };
        $scope.chkNewEntryForOld_selectedItemChanged = function () {
            $scope.regForm.ShowSearchBox = true;
                if($scope.regForm.NewEntryForOldValue == true){
                    $scope.regForm.ShowSearchBox = false;
                }
            };

        GetAll();
        $scope.Title = 'Registration';
        function GetAll() {
            ServiceFactory.getFormsList().success(function (data) {
                $scope.RegistrationForms = data;
                $scope.RegistrationForms_InitiateLookUps();

            }).error(function (data) {
                $scope.error = 'Unable to load Application Forms: ' + error.message;
            });
        }
        //     };
        $scope.RegistrationForms_InitiateLookUps = function () {
            $scope.RegistrationForms_GetLevels();
        };
        $scope.RegistrationForms_Get = function () {
            $scope.regForm = this.regForm;
            $('#regFormModal').modal('show');
        };//Get form
        $scope.RegistrationForms_Edit = function () {

            $scope.regForm = this.regForm;
            $scope.editMode = true;

            var birthdate = new date($scope.regForm.BirthDate);
            $scope.regForm.BirthDate = birthdate.normalDate;

            $('#regFormModal').modal('show');
        };//Edit form
        $scope.RegistrationForms_ConfirmOldNew = function () {
            this.RegForm = new Registration();
            $scope.regForm = this.RegForm;
            $scope.regForm.Type = '-Select-';

            $scope.editMode = false;
            $scope.regForm.newentryforold = false;

            $('#confirmOldNewModal').modal('show');
        };//Confirm old/new student modal

        $scope.RegistrationForms_ShowAdd = function () {
            $scope.editMode = false;
            $('#confirmOldNewModal').modal('hide');
            $('#regFormModal').modal('show');
        };//Add form modal

        $scope.RegistrationForms_Cancel = function () {
            $scope.regForm = null;
            $('#regFormModal').modal('hide');
        };// Cancel modal

        $scope.RegistrationForms_CancelOldNewModal = function () {
            $scope.regForm = null;
            $scope.regForm.newentryforold = false;

            $('#confirmOldNewModal').modal('hide');
        };// Cancel modal

        $scope.RegistrationForms_ShowDelete = function () {
            $scope.regForm = new Applicationform();
            $scope.deleteForm = true;
            $('#confirmModal').modal('show');
        };//Add form modal
        $scope.RegistrationForms_Delete = function () {
            currentForm = this.regForm;
            $('#confirmModal').modal('show');

            ServiceFactory.deleteForm(currentForm).success(function (data) {
                $('#confirmModal').modal('hide');
                //$scope.form.pop(currentForm);
                getAll();

            }).error(function (data) {
                $scope.error = "An Error has occured while Deleting item! " + data.ExceptionMessage;

                $('#confirmModal').modal('hide');
            });
        };// delete item
        $scope.RegistrationForms_Add = function () {
            //this.form = new Applicationform();
            var currentForm = $scope.regForm;
            if ($scope.addform.$valid) {
                ServiceFactory.addForm(currentForm).success(function (data) {
                    $scope.addMode = false;
                    currentForm.editMode = false;
                    currentForm.BirthDate = txtBdate.value;
                    $scope.RegistrationForms.push(currentForm);
                    $scope.regForm = null;//reset form

                    getAll();
                    alert('Item saved.');

                }).error(function (data) {
                    $scope.regForm.error = "An Error has occured while creating this form! " + data.ExceptionMessage;
                });
            } else {
                $scope.regForm.error = "There are invalid fields.";
            }
        };// add form data
        $scope.RegistrationForms_Update = function () {
            var currentForm = this.regForm;
            currentForm.BirthDate = txtBdate.value;
            ServiceFactory.updateForm(currentForm).success(function (data) {
                currentForm.editMode = false;

                $('#regFormModal').modal('hide');
            }).error(function (data) {
                $scope.error = "An Error has occured while updating form! " + data.ExceptionMessage;
            });
        };
        $scope.RegistrationForms_ShowConfirm = function (data) {
            $scope.regForm = data;
            $('#confirmModal').modal('show');
        };//updates form data
        $scope.RegistrationForms_GetLevels = function () {
            ServiceFactory.getLevelList().success(function (data) {
                $scope.Levels = {};
                $scope.Levels.SchoolLevels = data;

            }).error(function (data) {
                $scope.error = 'Unable to load levels: ' + error.message;
            });
        }; //get student levels
        //Registration
        $scope.Registrations_GetAll = function () {
            DeactivateDisplay();
            $scope.Registrations = true;
            $scope.Title = 'Registration';
            $scope.Registrations = new Registration();

        };
        //Enrollment
        $scope.Enrollment_GetAll = function () {
            DeactivateDisplay();
            $scope.Title = 'Enrollment';
        };

        //Sorting

        function DeactivateDisplay() {
            $scope.RegistrationForms = false;
            $scope.Registrations = false;
            $scope.Enrollment = false;
        } //resets all lists

    });


    /* End of controller */


    /* Start of Service factory */

    //Application form factory

    regForm.factory('ServiceFactory', function ($http) {
        return {
            getFormsList: function () {
                url = baseAddress + "applicationform/GetFormsList";
                return $http.get(url);
            },
            getForm: function (regForm) {
                url = baseAddress + "applicationform/GetForm/" + regForm.ID;
                return $http.get(url);
            },
            deleteForm: function (regForm) {
                //url = baseAddress + 'applicationform/DeleteForm/' + form.ID;
                //return $http.delete(url);
                return $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: baseAddress + 'applicationform/DeleteForm/' + regForm.ID,
                    data: regForm,
                });
            },
            addForm: function (regForm) {
                //ajax is readable by jobject if dotnet
                return $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: baseAddress + 'applicationform/AddForm/',
                    data: regForm,
                });
            },

            updateForm: function (regForm) {
                //ajax is readable by jobject if dotnet
                return $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: baseAddress + 'applicationform/UpdateForm/' + regForm.ID,
                    data: regForm,
                });
            },
            //url = baseAddress + "UpdateForm/" + form.ID;
            //return $http.put(url); Boom! not working,..   

            getLevelList: function (Levels) {
                url = baseAddress + "level/Get/";
                return $http.get(url);
            }
        };
    });

    /* End of Service Factory*/

})();
